package com.cameron.serviceedu.schedule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cameron.serviceedu.entity.EduCourse;
import com.cameron.serviceedu.service.EduCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

import static com.cameron.constant.CourseConstant.NOT_COMPLETE;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2023/01/17/14:15
 */
@Component
@Slf4j
public class CourseSchedule {

    @Resource
    private EduCourseService eduCourseService;


    @Scheduled(fixedDelay = 10 * 60 * 1000)
    public void delIncomplete() {
        log.info("自动清除系统启动...");

        int count = eduCourseService.count(
                new LambdaQueryWrapper<EduCourse>()
                        .eq(EduCourse::getStatus, NOT_COMPLETE)
                        .le(EduCourse::getGmtCreate, new Date(System.currentTimeMillis() - 30 * 60 * 1000))
                        .last("limit 1"));
        if (count < 1) {
            log.info("无需清理");
            return;
        }
        LambdaQueryWrapper<EduCourse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduCourse::getStatus, NOT_COMPLETE);
        eduCourseService.remove(wrapper);
        log.info("清除完毕");
    }
}
